import { Base64 } from "../../utils/base64";
import { randomStr, getTagData, isMobile, randonNum } from "../../utils/util";

// pages/user/enroll/enro.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sendLoading: 0,
    formData: {
      name: '',
      loginName: '',
      loginPwd: '',
      captcha: ''
    },
    rules: [
      {
        name: 'name',
        rules: {required: true, message: '请输入名字'},
    },
    {
            name: 'loginName',
            rules: {required: true, message: '请输入手机号'},
        }, {
            name: 'loginPwd',
            rules: {required: true, message: '请输入密码'},
        },{
            name: 'captcha',
            rules: {required: true, message: '请输入验证码'},
        }
    ]
  },

  tapEyes: function() {
    this.setData({showPwd: !this.data.showPwd});
  },

  formInputChange(e) {
    const {field} = getTagData(e);
    const {formData} = this.data;
    formData[field] = e.detail.value
    this.setData({formData})
  },

  submitForm() {
    this.selectComponent('#form').validate((valid, errors) => {
        console.log('valid', valid, errors)
        if (!valid) {
            const firstError = Object.keys(errors)
            if (firstError.length) {
                wx.showToast({
                  title: errors[firstError[0]].message,
                  icon: 'none'
                })
            }
        } else {
          const payload = {...this.data.formData};
          const arrayPwd = Array.from(randonNum(3) + payload.loginPwd, char => char.charCodeAt(0));
          if(arrayPwd.some(a => a > 0xFF)) {
            wx.showToast({
              title: '不支持特殊密码',//123122@ MTIzMTIyQA==
              icon: 'none'
            })
            return;
          }
          payload.loginPwd = Base64.base64EncArr(arrayPwd);
          wx.$http.post({url: '/enroll', data: payload, noToken: true}).then(res => {
               wx.showToast({
                 title: '已注册,请登录',
               });
               setTimeout(() => {
                wx.reLaunch({
                  url: '/pages/mine/index',
                })
               }, 2500);
          }).catch(err => {
            const {code} = err;
            if(code === 10002) {
              wx.showToast({
                title: '手机号已注册',
                icon: 'none'
              })
            }
          })
        }
    });
  },

  tapSendSms() {
    if(this.data.sendLoading > 0) {
      return;
    }
    if(!isMobile(this.data.formData.loginName)) {
      wx.showToast({
        title: '请输入手机号',
        icon:'none'
      })
      return;
    }
    this.setData({sendLoading: 60}, () => this.sendLoadingCount())
    this.data.formData.uuid = randomStr();
    const {loginName, uuid} = this.data.formData;
    wx.$http
      .post({url: "/szm/captcha.sms", data: { mobile: loginName, uuid: uuid, type: 2 }, noToken: true})
      .then(() => {
        wx.showToast({
          title: '发送短信',
        })
      })
      .catch(() => this.setData({sendLoading: 0}))
  },

  sendLoadingCount() {
    this.intervalId = setInterval(() => {
      let {sendLoading} = this.data;
      if (sendLoading > 0) {
        --sendLoading;
        this.setData({sendLoading})
      } else {
        clearInterval(this.intervalId);
      }
    }, 1000);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    clearInterval(this.intervalId)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})